/*显示隐藏密码*/
function showPass() {
    if ($("#PASSWORD").attr("type") == "password") {
        $("#PASSWORD").attr("type", "text");
        $("#showPass").css({
            color: "black"
        });
    } else {
        $("#PASSWORD").attr("type", "password");
        $("#showPass").css({
            color: "blue"
        });
    }
}

//---------------------------------------------------------------------------------------------------------------------

//输入部门编码和名称进行模糊添加
function departmentAdd() {
    var department_data = {
        keywords: $("#BIANMA").val()
    }
    var succCallBack = function (data, status, response) {
        if (data.departmentList != "") {
            var jsonObj = data.bianmaList;
            if (jsonObj == "") {
                $("#tuan").html("<li> 请输入部门编码进行添加</li> ");
            } else {
                var optionstring = "";
                for (var i = 0; i < jsonObj.length; i++) {
                    optionstring += "<li onclick='selectDepartment(\"" + jsonObj[i].BIANMA + "\")'>" + jsonObj[i].BIANMA + jsonObj[i].DEPARTMENT_NAME + "</li>";
                }
                $("#tuan").html(optionstring);
            }
        } else {
            $("#tuan").html("<li>请输入部门编码进行添加</li> ");
        }
    }
    web.doAjax(root.accessInter.access_department_listDepartment, 'post', department_data, succCallBack);
}

function selectDepartment(BIANMA) {
    $("#BIANMA").val(BIANMA);
    $("#tuan").hide()
}

//运营经理获取光标
$("#BIANMA").focus(function () {
    $("#tuan").show()
});
$(document).bind("click", function (e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if ($(e.target).closest("#tuan").length == 0 && $(e.target).closest("#BIANMA").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan").hide()
    }
})

//---------------------------------------------------------------------------------------------------------------------------

/*获取当前时间，并格式化为“yyyy-MM-dd HH:MM:SS”*/
function get_time() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
    return currentdate;
}

//------------------------编辑，从主页面跳转过来的----------------------------------------------------------------------------------------------

var user_editData = JSON.parse(window.session_storage.getItem('user_editData'));
/*user_editData为true时为编辑*/
var b = new Base64();
if (user_editData.USER_ISEIDT == true) {
    var NAME = $('#NAME').val(user_editData.NAME);
    var NUMBER = $('#NUMBER').val(user_editData.NUMBER);
    var ENTRY_TIME = $('#ENTRY_TIME').val(user_editData.ENTRY_TIME);
    var USERNAME = $('#USERNAME').val(user_editData.USERNAME);
    var PHONE = $('#PHONE').val(user_editData.PHONE);
    var EMAIL = $('#EMAIL').val(user_editData.EMAIL);
    var DEPARTMENT_NAME = $('#DEPARTMENT_NAME').val(user_editData.DEPARTMENT_NAME);
    var BIANMA = $('#BIANMA').val(user_editData.BIANMA);
    var PASS = b.decode(user_editData.PASSWORD);
    var PASSWORD = $('#PASSWORD').val(PASS);
    var STATE = $('#STATE').val(user_editData.STATE);

    var role_name = JSON.parse(sessionStorage.getItem("roleNameArr"));
    var role_id = JSON.parse(sessionStorage.getItem("roleIdArr"));
    var roleHtmlStr = "";
    for (var i = 0; i < role_name.length; i++) {
        roleHtmlStr += role_name[i] + " ";
    }
    $(".roleName").html(roleHtmlStr);
    if (user_editData.STATE == 0) {
        $("#STATUS").prop("checked", true)
    } else {
        $("#STATUS").prop("checked", false)
    }
    $("#accounts").validate({
        /*rules: {
            NAME: {
                required: true,
                minlength: 2,
                maxlength: 20,
            },
            NUMBER: {
                required: true,
                isDigits: true,
                maxlength: 12,
                minlength: 3
            },
            ENTRY_TIME: {
                required: true,
            },
            USERNAME: {
                required: true,
                isUserName: true,
            },
            PASSWORD: {
                required: true,
                minlength: 3,
                maxlength: 16
            },
            PHONE: {
                required: true,
                isMobile: true,
            },
            EMAIL: {
                email: true,
            },
            BIANMA: {
                isDigits: true,
                maxlength: 22
            },
        },*/
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler: function (form) {
            var accountStatus;
            if ($("#STATUS")[0].checked == true) {
                accountStatus = 0;
            } else {
                accountStatus = 1;
            }
            $(form).ajaxSubmit({
                type: 'post',
                url: root.accessInter.access_user_editUserApi,
                data: {
                    NUMBER: $('#NUMBER').val(), //工号
                    NAME: $('#NAME').val(),     //  姓名
                    ENTRY_TIME: $('#ENTRY_TIME').val(),//入职时间
                    USERNAME: $('#USERNAME').val(),       //  账号
                    PASSWORD: $('#PASSWORD').val(),     //  密码
                    PHONE: $('#PHONE').val(),        //  电话
                    EMAIL: $('#EMAIL').val(),        //  邮箱
                    ROLE_ID: sessionStorage.getItem("roleIdArr"),       //  角色
                    BIANMA: $('#BIANMA').val(), //部门 编码
                    STATUS: accountStatus, //  状态
                    USER_ID: user_editData.USER_ID
                },  //传入组装的参数
                success: function (data) {
                    switch (data.code) {
                        case 200:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                            window.session_storage.setItem('account_add','');
                            var index = parent.location.reload();
                            parent.layer.close(index);
                        case 400:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                        case 404:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                    }
                },
            });
        }
    });

//添加账号，从主页面跳转过来的--------------------------------------------------------------------------------------------------------------
} else {
    var role_name = JSON.parse(sessionStorage.getItem("roleNameArr"));
    var role_id = JSON.parse(sessionStorage.getItem("roleIdArr"));
    var user_editDataAdd = JSON.parse(window.session_storage.getItem('user_editDataAdd'));
    var roleHtmlStr = "";
    for (var i = 0; i < role_name.length; i++) {
        roleHtmlStr += role_name[i] + " ";
    }
    $(".roleName").html(roleHtmlStr);
    var enter_time = get_time();
    $("#accounts").validate({
        rules: {
            NAME: {
                required: true,
                minlength: 2,
                maxlength: 20,
            },
            NUMBER: {
                required: true,
                isDigits: true,
                maxlength: 12,
                minlength: 3
            },
            ENTRY_TIME: {
                required: true,
            },
            USERNAME: {
                required: true,
                isUserName: true,
            },
            PASSWORD: {
                required: true,
                minlength: 3,
                maxlength: 16
            },
            PHONE: {
                required: true,
                isMobile: true,
            },
            EMAIL: {
                email: true,
            },
            BIANMA: {
                isDigits: true,
                maxlength: 22
            },
        },
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler: function (form) {
            var accountStatus;
            if ($("#STATUS")[0].checked == true) {
                accountStatus = 0;
            } else {
                accountStatus = 1;
            }
            $(form).ajaxSubmit({
                type: 'post',
                url: root.accessInter.access_user_saveUApi,
                data: {
                    NUMBER: $('#NUMBER').val(), //工号
                    NAME: $('#NAME').val(),  //  姓名
                    USERNAME: $('#USERNAME').val(),       //  账号
                    PASSWORD: $('#PASSWORD').val(),     //  密码
                    PHONE: $('#PHONE').val(),        //  电话
                    EMAIL: $('#EMAIL').val(),        //  邮箱
                    ROLE_ID: sessionStorage.getItem("roleIdArr"),       //  角色
                    BIANMA: $('#BIANMA').val(),
                    STATUS: accountStatus //  状态
                },  //传入组装的参数
                success: function (data) {
                    switch (data.code) {
                        case 200:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                            window.session_storage.setItem('account_add','');
                            var index = parent.location.reload();
                            parent.layer.close(index);
                        case 400:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                        case 404:
                            layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                    }
                },
            });
        }
    });
}

//----------------------------------------------------------------------------------------------------------------------
/*角色信息编辑*/
function account_form(title, url, w, h) {
    layer_show(title, url, w, h);
    //更改页面缓存
    var account_add = {
        NUMBER: $('#NUMBER').val(),
        DEPARTMENT_NAME: $('#DEPARTMENT_NAME').val(),
        PHONE: $('#PHONE').val(),
        EMAIL: $('#EMAIL').val(),
        STATE: $('#STATE').val(),
        NAME: $('#NAME').val(),
        USERNAME: $('#USERNAME').val(),
        BIANMA: $('#BIANMA').val(),
        USER_ID: user_editData.USER_ID,
        PASSWORD: $('#PASSWORD').val(),
        ENTRY_TIME: $('#ENTRY_TIME').val(),
        USER_FORM: true,
    };
    window.session_storage.setItem('account_add', JSON.stringify(account_add));
}

//----------------------------------------------------------------------------------------------------------------------
